package cn.otrue.patienthealthmanager.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import cn.otrue.patienthealthmanager.R;
import cn.otrue.patienthealthmanager.activity.HealthPlanActivity;
import cn.otrue.patienthealthmanager.view.CalendarViewPlan;
import cn.otrue.patienthealthmanager.view.SwitchButton;

public class DrugWeekPlanAdapter extends BaseAdapter {

	private List<Map<String, String>> data;
	private LayoutInflater inflater;
	private Context context;

	public DrugWeekPlanAdapter(Context context, List<Map<String, String>> data) {
		super();
		this.context = context;
		this.data = data;
		inflater = LayoutInflater.from(this.context);
	}

	@Override
	public int getCount() {

		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		View view = inflater.inflate(R.layout.item_main_plan, null);
		TextView tv_main_plan_name = (TextView) view
				.findViewById(R.id.tv_main_plan_name);
		CheckBox cb_main_plan_ring = (CheckBox) view
				.findViewById(R.id.cb_main_plan_ring);

		final CalendarViewPlan ll_main_plan_calendar = (CalendarViewPlan) view
				.findViewById(R.id.cvp_main_plan_calendar);

		ll_main_plan_calendar.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				Intent intent = new Intent();
				intent.setClass(context, HealthPlanActivity.class);
				context.startActivity(intent);
				return false;
			}
		});

		final SwitchButton sb_main_switch = (SwitchButton) view
				.findViewById(R.id.sb_main_switch);

		Map<String, String> eachMap = data.get(arg0);

		tv_main_plan_name.setText(eachMap.get("name"));

		if (eachMap.get("ring").equals("true")) {
			cb_main_plan_ring.setChecked(true);
		} else {
			cb_main_plan_ring.setChecked(false);
		}

		if (!eachMap.get("open").equals("true")) {
			sb_main_switch.openButton(false);
			ll_main_plan_calendar.setVisibility(View.GONE);
		} else {
			sb_main_switch.openButton(true);
			ll_main_plan_calendar.setVisibility(View.VISIBLE);
		}

		sb_main_switch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				sb_main_switch.changeOpen();
				if (sb_main_switch.open) {
					ll_main_plan_calendar.setVisibility(View.VISIBLE);
				} else {
					ll_main_plan_calendar.setVisibility(View.GONE);
				}

			}
		});

		return view;
	}
}
